package com.fruit.auth.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.validator.constraints.Email;

import com.fruit.system.model.Menu;

@Table(name="AUTH_USER")
@Entity
@Setter
@Getter
@DynamicUpdate
public class User implements Serializable
{

	private static final long serialVersionUID = -7568435584504748659L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Size(min=4,max=32,message="用户名长度必须在4到32位之间")
	@Column(updatable=false,nullable=false,length=32,unique=true)
	@NotNull(message="用户名不能为空")
	private String username;
	
	@Column(updatable=false,nullable=false,length=128)
	@NotNull(message="密码不能为空")
	@Size(min=6,max=32,message="密码长度必须在6到32位之间")
	private String password;
	
	@Column(length=64)
	private String nickname;//昵称
	
	private Integer sex;//性别
	
	@Email(message="请输入合法的邮箱地址")
	@Column(length=64)
	private String email;//邮箱
	
	@Column(length=30,unique=true)
	private String phone;//联系电话
	
	@Column(updatable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime=new Date();//注册时间
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastLoginTime;//上一次登陆时间
	
	@Column(length=15)
	private String lastLoginIp;//上一次登陆IP地址
	
	//认证是失败次数，认证失败累加，成功后清零。达到设定失败次数后锁定帐号，防止无限制次数尝试猜测密码
	@Column(columnDefinition="tinyint default 0")
	private Integer loginFailureTimes=0;
	
	//首次激活账号或用于找回密码设定的随机UUID字符串
	private String randomUUID;
	
	//用户所属部门
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="AUTH_DEPARTENT_id")
	private Department department;
	
	//private List
	//每个权限位对应的权限码总和，二进制或‘|’运算结果
	@Transient
	private long[] privilegeSum;
	
	//是否是超级管理员
	@Transient
	private boolean isAdmin;
	
	/**  计算权限总和*/
	public void calcPrivilege(List<Privilege> privileges)
	{
		int privilegePos=0;
		Long privilegeCode=0L;
		if(privileges!=null)
		{
			for(Privilege privilege:privileges)
			{
				privilegePos=privilege.getPrivilegePos();
				privilegeCode=privilege.getPrivilegeCode();
				privilegeSum[privilegePos]=privilegeSum[privilegePos]|privilegeCode;
			}
			privileges=null;
		}
		
	}
	/**
	 * 判断是否有权限访问该资源
	 * @param privilege 要访问的资源所对应的权限
	 */
	public boolean hasPrivilege(Privilege privilege)
	{
		if(isAdmin) return true;
		if(privilege!=null&&privilegeSum!=null)
		{
			int pos=privilege.getPrivilegePos();
			long access=privilegeSum[pos] & privilege.getPrivilegeCode();
			return access==0?false:true;
		}
		return false;
	}
	
	public boolean hasPrivilege(Menu menu)
	{
		if(isAdmin) return true;
		
		if(menu!=null&&privilegeSum!=null)
		{
			int pos=menu.getMenuPos();
			long access=privilegeSum[pos] & menu.getMenuCode();
			return access==0?false:true;
		}
		return false;
	}
	public User(Integer id, String username, String password, String nickname)
	{
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.nickname = nickname;
	}
	public User()
	{
		
	}
	
	
}
